YX.fn.EncryptedMessage = function() {
	$("body").on("click", "#encryption-no", this.encryptionYes.bind(this))
	$("body").on("click", "#encryption-yes", this.encryptionNo.bind(this))

	//修改加密范围
	$("body").on("click", ".em-edit", this.resetVisible.bind(this))

}

YX.fn.resetVisible = function() {
	//可见对象
/*	var receivers = []
	for(var key in chooseUserData.checkedUsers) {
		//只遍历对象自身的属性，而不包含继承于原型链上的属性。  
		if(chooseUserData.checkedUsers.hasOwnProperty(key) === true) {
			receivers.push(key);
		}
	}*/
	
	var msg = this.cache.findMsg(this.crtSession,chooseUserData.msgid)

/*	var scene = this.crtSessionType,
		to = this.crtSessionAccount,
		text = msg.text,
		//@的对象
		account = []
	if(msg.apns) {
		if(msg.apns.accounts) {
			account = msg.apns.accounts
		}
	}*/



	this.nim.deleteMsg({
		msg: msg,
		done: function(err) {
			//options.$trigger.addClass('j-msg')
			if(err) {
				if(err.code === 508) {
					alert('发送时间超过2分钟的消息，不能被撤回')
				} else {
					alert(err.message || '操作失败')
				}
			} else {
				//删除原来的msg
				this.editMsg(chooseUserData.msgid)
				//重发消息
				//this.mysdk.sendTextMessage(scene, to, text, receivers, account, this.sendMsgDone.bind(this))
			}
		}.bind(this)
	})

}

//点击取消加密
YX.fn.encryptionNo = function() {
	if($("#encryption-no").hasClass("hideDiv")) {
		$("#encryption-no").toggleClass("hideDiv")
		$("#encryption-yes").toggleClass("hideDiv")
	}
	chooseUserData.checkedUsers = {};
}

//点击加密
YX.fn.encryptionYes = function() {
	var that = this
	if($("#encryption-yes").hasClass("hideDiv")) {
		$("#encryption-no").toggleClass("hideDiv")
		$("#encryption-yes").toggleClass("hideDiv")
	}

	//弹出选择加密对象模态框
	$("#encryptedMsgModal").load("../zdy-view/EncryptedMessage.html", function() {
		//参考team.js404行
		var teamId = that.crtSessionAccount
		that.getTeamMembersById1(teamId)

		Modal.show("#encryptedMsgModal")
	});

}

//修改加密范围
YX.fn.Editencryption = function(msgid) {
	var that = this

	//弹出选择加密对象模态框
	$("#encryptedMsgModal").load("../zdy-view/EncryptedMessage.html", function() {
		//参考team.js404行
		var teamId = that.crtSessionAccount
		that.getTeamMembersById(teamId,msgid)
		Modal.show("#encryptedMsgModal")
	});

}

YX.fn.getTeamMembersById1 = function(teamID) {
	var that = this
	this.mysdk.getTeamMembers(teamID, function(err, obj) {
		if(!err) {
			that.cache.setTeamMembers(teamID, obj)
			that.renderTeamUsers(obj)
		} else {
			alert(err.message)
		}
	})
}

YX.fn.getTeamMembersById = function(teamID, msgid) {
	var that = this
	this.mysdk.getTeamMembers(teamID, function(err, obj) {
		if(!err) {
			that.cache.setTeamMembers(teamID, obj)
			that.renderTeamUsersAndReceivers(obj, msgid)
		} else {
			alert(err.message)
		}
	})
}

//渲染群加密对象
YX.fn.renderTeamUsersAndReceivers = function(obj, msgid) {
	var that = this
	var members = obj.members
	//清空选择
	chooseUserData.currentUsers = {}
	chooseUserData.checkedUsers = {}
	chooseUserData.msgid = msgid
	
	var msg = this.cache.findMsg(this.crtSession, msgid)
	var custom = JSON.parse(msg.custom)
	var receivers = custom.receivers
	for(var i = 0; i < members.length; i++) {
		var member = members[i]

		var account = member.account,
			ifencrypted = "", //是否是加密对象
			avatar = getAvatar(that.cache.getUserById(account) ? that.cache.getUserById(account).avatar : ""),
			nick = getNick(account)
		chooseUserData.currentUsers[member.account] = member
			//加密对象
		if($.inArray(account, receivers) > -1) {
			chooseUserData.checkedUsers[account] = member
			ifencrypted = "checked='ckecked'"
		}
		var $trObj = $(`<tr><td>
					<input type = 'checkbox' ` + ifencrypted + ` data-userid = '` + account + `' id = 'user-` + account + `' class = 'checkbox-old' >
					<label class = 'checkbox-new' for = 'user-` + account + `' > </label>
				</td> 
				<td><img src = '` + avatar + `' class = 'zdy-userImg' ></td>
				<td>
					<p class = 'userName' > ` + nick + ` </p> 
				</td> </tr>`);
		$("#em-user-wrapper-table-data").append($trObj);
	}
}

//渲染群成员
YX.fn.renderTeamUsers = function(obj) {
	var that = this
	var members = obj.members
	chooseUserData.currentUsers = {};

	for(var i = 0; i < members.length; i++) {
		var member = members[i]
		var account = member.account,
			avatar = getAvatar(that.cache.getUserById(account) ? that.cache.getUserById(account).avatar : ""),
			nick = getNick(account)
		chooseUserData.currentUsers[member.account] = member
		var $trObj = $(`<tr><td>
					<input type = 'checkbox' data-userid = '` + account + `' id = 'user-` + account + `' class = 'checkbox-old' >
					<label class = 'checkbox-new' for = 'user-` + account + `' > </label>
				</td> 
				<td><img src = '` + avatar + `' class = 'zdy-userImg' ></td>
				<td>
					<p class = 'userName' > ` + nick + ` </p> 
				</td> </tr>`);
		$("#em-user-wrapper-table-data").append($trObj);
	}
}